﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.CommonDao;
using CSP.Dao.IObjectDaos.CSP;
using CSP.DataAccess;
using CSP.Common.Tools;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class SearchInfoDao : CSPCommonDao, ISearchInfoDao
    {
        public SearchInfoDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        { }

        public List<int> FindServiceIdsByServiceTypeIdAndServiceIds(List<int> serviceTypeIds, List<int> serviceIds)
        {
            List<int> result = new List<int>();
            IQueryable<int> ids = null;
            if (serviceIds != null && serviceIds.Count != 0)
            {
                ids = _context.SearchInfoes.Where(r => serviceTypeIds.Contains(r.ServiceTypeId.Value) && serviceIds.Contains(r.ServiceId)).Select(r => r.ServiceId);
            }
            else
            {
                ids = _context.SearchInfoes.Where(r => serviceTypeIds.Contains(r.ServiceTypeId.Value)).Select(r => r.ServiceId);
            }

            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }

            return result;
        }

        public List<int> FindServiceIdsByProvinceIdAndServiceIds(int province, List<int> serviceIds)
        {
            List<int> result = new List<int>();
            IQueryable<int> ids = null;
            if (serviceIds != null && serviceIds.Count != 0)
            {
                ids = _context.SearchInfoes.Where(r => r.Province == province && serviceIds.Contains(r.ServiceId)).Select(r => r.ServiceId);
            }
            else
            {
                ids = _context.SearchInfoes.Where(r => r.Province == province).Select(r => r.ServiceId);
            }

            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }

            return result;
        }

        public List<int> FindServiceIdsByCityIdAndServiceIds(int city, List<int> serviceIds)
        {
            List<int> result = new List<int>();
            IQueryable<int> ids = null;
            if (serviceIds != null && serviceIds.Count != 0)
            {
                ids = _context.SearchInfoes.Where(r => r.City == city && serviceIds.Contains(r.ServiceId)).Select(r => r.ServiceId);
            }
            else
            {
                ids = _context.SearchInfoes.Where(r => r.City == city).Select(r => r.ServiceId);
            }

            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }

            return result;
        }

        public List<int> FindServiceIdsByCountyIdAndServiceIds(int county, List<int> serviceIds)
        {
            List<int> result = new List<int>();
            IQueryable<int> ids = null;
            if (serviceIds != null && serviceIds.Count != 0)
            {
                ids = _context.SearchInfoes.Where(r => r.County == county && serviceIds.Contains(r.ServiceId)).Select(r => r.ServiceId);
            }
            else
            {
                ids = _context.SearchInfoes.Where(r => r.County == county).Select(r => r.ServiceId);
            }

            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }

            return result;
        }

        public List<int> FindServiceIdsByStartAgeAndEndAgeAndServiceIds(int startAge, int endAge, List<int> serviceIds)
        {
            List<int> result = new List<int>();
            IQueryable<int> ids = null;
            if (serviceIds != null && serviceIds.Count != 0)
            {
                ids = _context.SearchInfoes.Where(r => r.AgeStart >= startAge && r.AgeEnd <= endAge && serviceIds.Contains(r.ServiceId)).Select(r => r.ServiceId);
            }
            else
            {
                ids = _context.SearchInfoes.Where(r => r.AgeStart >= startAge && r.AgeEnd <= endAge).Select(r => r.ServiceId);
            }

            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }

            return result;
        }

        public List<int> FindServiceIdsBySearchValueAndServiceIds(string searchValue, List<int> serviceIds)
        {
            List<int> result = new List<int>();
            IEnumerable<int> ids = null;
            List<string> segmentationTerms = CommonTool.GetSegmentationTermsByKeyWords(searchValue.Trim());

            if (serviceIds == null)
            {
                serviceIds = new List<int>();
            }

            String[] temServiceIds = serviceIds.ConvertAll<string>(delegate(int s) { return s.ToString(); }).ToArray();
            //ids = _context.Database.SqlQuery<int>("QueryBySearchValueAndKeyWords @serviceIds ,@firstSearchValue,@secondSearchValue,@thirdSearchValue",
            //    String.Join(",", temServiceIds), segmentationTerms[0], segmentationTerms[1], segmentationTerms[2]);

            ids = _context.Database.SqlQuery<int>("QueryBySearchValueAndKeyWords {0} ,{1},{2},{3}",
                String.Join(",", temServiceIds), segmentationTerms[0], segmentationTerms[1], segmentationTerms[2]);
            if (ids.Count() != 0)
            {
                result = ids.ToList();
            }

            return result;
        }
    }
}
